﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ShiQuan.Hanker
{
    /// <summary>
    /// 海康摄像头
    /// </summary>
    public class HikCameraHelper:IDisposable
    {
        private DeviceInfo _deviceInfo = new DeviceInfo();
        /// <summary>
        /// 设备信息
        /// </summary>
        public DeviceInfo DeviceInfo
        {
            get { return this._deviceInfo; }
        }

        private int _userId = -1;
        /// <summary>
        /// 登录用户标识
        /// </summary>
        public int UserId { get { return this._userId; } }

        private List<ChannelInfo> _Channels = new List<ChannelInfo>();
        /// <summary>
        /// 设备通道状态
        /// </summary>
        public List<ChannelInfo> Channels
        {
            get { return this._Channels; }
        }
        private HCNetSDK.NET_DVR_DEVICEINFO_V30 mDeviceInfo = new HCNetSDK.NET_DVR_DEVICEINFO_V30();
        private HCNetSDK.NET_DVR_IPPARACFG_V40 mIpParaCfgV40 = new HCNetSDK.NET_DVR_IPPARACFG_V40();
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool Login(string serverIp,int serverPort,string userName,string password,out string msg)
        {
            msg = string.Empty;
            
            this._userId = HikDVRHelper.Login(serverIp, serverPort, userName, password,ref mDeviceInfo, out msg);
            if(this._userId < 0)
                return false;
            /*初始设备信息*/
            this._deviceInfo = new DeviceInfo()
            {
                ServerIP = serverIp,ServerPort = serverPort,UserName=userName,Password = password
            };
            if (HikDVRHelper.GetDeviceInfo(this.UserId, ref _deviceInfo, out msg) == false)
                return false;

            if (HikDVRHelper.GetDeviceInfo(this.UserId, ref _deviceInfo, out msg) == false)
                return false;

            if (HikDVRHelper.GetNetworkInfo(this.UserId, ref this._deviceInfo, out msg) == false)
                return false;

            return HikDVRHelper.GetChannelList(this._userId, mDeviceInfo, mIpParaCfgV40, out _Channels, out msg);
        }

        #region 抓拍
        /// <summary>
        /// 抓拍
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool Capture(int channelIndex,out string msg)
        {
            msg = string.Empty;
            var filePath = Application.StartupPath + "\\Capture\\" + channelIndex + "";
            if (System.IO.Directory.Exists(filePath) == false)
                System.IO.Directory.CreateDirectory(filePath);
            var fileName = filePath + "\\" + DateTime.Now.ToString("yyyyMMdd-HHmmss") + ".jpg";
            return this.Capture(channelIndex,fileName, out msg);
        }

        /// <summary>
        /// 抓拍
        /// </summary>
        /// <param name="saveFile"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool Capture(int channelIndex, string saveFile, out string msg)
        {
            return HikDVRHelper.Capture(this.UserId, channelIndex, saveFile, out msg);
        }

        /// <summary>
        /// 抓拍
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool CaptureBMP(PreviewModel model, int readHandle,int port, out string msg)
        {
            msg = string.Empty;
            var filePath = Application.StartupPath + "\\Capture\\" + readHandle + "";
            if (System.IO.Directory.Exists(filePath) == false)
                System.IO.Directory.CreateDirectory(filePath);
            var fileName = filePath + "\\" + DateTime.Now.ToString("yyyyMMdd-HHmmss") + ".bmp";
            if (model == PreviewModel.Instant)
                return this.CaptureBMP(readHandle, fileName, out msg);
            else
                return this.CaptureBMP(readHandle, port, fileName, out msg);
        }

        /// <summary>
        /// 抓拍-直流抓拍
        /// </summary>
        /// <param name="saveFile"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool CaptureBMP(int readHandle, string saveFile, out string msg)
        {
            return HikDVRHelper.CaptureBMP(readHandle, saveFile, out msg);
        }

        /// <summary>
        /// 抓拍-回滚转码
        /// </summary>
        /// <param name="saveFile"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool CaptureBMP(int readHandle, int port, string saveFile, out string msg)
        {
            return HikDVRHelper.CaptureBMP(readHandle, port, saveFile, out msg);
        }
        #endregion

        #region 录像
        /// <summary>
        /// 开始录像
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool BeginRecord(int channelIndex, int readHandle, out string msg)
        {
            msg = string.Empty;
            var filePath = Application.StartupPath + "\\Record\\" + channelIndex;
            if (System.IO.Directory.Exists(filePath) == false)
                System.IO.Directory.CreateDirectory(filePath);
            var fileName = System.IO.Path.Combine(filePath, DateTime.Now.ToString("yyyyMMdd-HHmmss") + ".mp4");

            return this.BeginRecord(channelIndex,readHandle, fileName, out msg);
        }
        /// <summary>
        /// 开始录像
        /// </summary>
        /// <param name="saveFile"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool BeginRecord(int channelIndex, int readHandle, string saveFile, out string msg)
        {
            return HikDVRHelper.BeginRecord(this.UserId, channelIndex, readHandle, saveFile, out msg);
        }
        /// <summary>
        /// 结束录像
        /// </summary>
        /// <param name="readHandle"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool StopRecord(int readHandle, out string msg)
        {
            return HikDVRHelper.StopRecord(readHandle, out msg);
        }
        #endregion
        /// <summary>
        /// 注销
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool Logout(out string msg)
        {
            return HikDVRHelper.Logout(this.UserId, out msg);
        }
        /// <summary>
        /// 销毁资源
        /// </summary>
        public void Dispose()
        {
            if(this.UserId >=0)
            {
                var msg = string.Empty;
                HikDVRHelper.Logout(this.UserId, out msg);
            }
        }
    }
}
